<template>
  <el-dialog
    :visible.sync="Dialog"
    title="编辑员工"
    :before-close="handleClose"
    class="mydialog"
  >
    <el-form
      :model="formData"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
      :label-position="labelPosition"
    >
      <el-form-item label="姓名:" prop="username">
        <el-input
          v-model="formData.username"
          placeholder="1-30个字符"
          style="width: 50%;"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机:" prop="mobile">
        <el-input
          v-model="formData.mobile"
          placeholder="请输入手机号"
          style="width: 40%;"
        ></el-input>
      </el-form-item>
      <el-form-item label="入职时间:" prop="timeOfEntry">
        <div class="block">
          <el-date-picker
            v-model="formData.timeOfEntry"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="聘用形式:" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment">
          <el-option
            v-for="item in employees.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号:" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          style="width: 50%;"
          placeholder="1-20个字符"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门:" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          style="width: 40%;"
          placeholder="请选择"
          @focus="openTree"
        >
        </el-input>
        <el-tree
          class="mytree"
          v-if="treeshow"
          :data="treeList"
          default-expand-all
          :props="defaultProps"
          @node-click="handleNodeClick"
        ></el-tree>
      </el-form-item>
      <el-form-item class="btn">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getTreeList } from '../../../api/departments'
import employees from '../../../utils/constant/employess'
import { addEmpListApi } from '../../../api/employees'
export default {
  props: ['Dialog'],
  data () {
    return {
      employees,
      labelPosition: 'left',
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          {
            min: 1,
            max: 30,
            message: '请输入1-30个字符',
            trigger: 'blur'
          }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ]
      },
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        timeOfEntry: '',
        departmentName: ''
      },
      treeshow: false,
      treeList: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  methods: {
    submit () {
      addEmpListApi(this.formData)
      this.$emit('getEmpList')
      this.$message({
        type: 'info',
        message: '新增成功'
      })
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.handleClose()
    },
    handleNodeClick (e) {
      this.formData.departmentName = e.name
      this.closeTree()
      this.$emit('closed')
    },
    handleClose () {
      this.$emit('closed')
    },
    closeTree () {
      this.treeshow = false
    },
    openTree () {
      this.treeshow = true
    },
    async trunData () {
      const res = await getTreeList()
      const newList = res.depts.filter(value => {
        return value.pid === ''
      })
      newList.map(value => {
        const children = []
        res.depts.forEach(values => {
          if (value.id === values.pid) {
            children.push(values)
          }
          value.children = children
        })
        return value
      })
      this.treeList = newList
    }
  },
  created () {
    this.trunData()
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__body {
  padding: 30px 40px;
  .btn {
    display: flex;
    justify-content: center;
    margin-left: -100px;
  }
  .mytree {
    border: 1px solid #999;
    position: absolute;
    border-radius: 5px;
    width: 40%;
    z-index: 1;
  }
}
</style>
